<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="800px">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="督导标题" prop="supervisionTitle">
        <el-input v-model="formData.supervisionTitle" placeholder="请输入督导标题" />
      </el-form-item>
      <el-form-item label="督导类型" prop="supervisionType">
        <el-select v-model="formData.supervisionType" placeholder="请选择督导类型">
          <el-option label="现场督导" :value="1" />
          <el-option label="远程督导" :value="2" />
          <el-option label="专项督导" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="督导对象" prop="supervisionObject">
        <el-input v-model="formData.supervisionObject" placeholder="请输入督导对象" />
      </el-form-item>
      <el-form-item label="督导时间" prop="supervisionTime">
        <el-date-picker
          v-model="formData.supervisionTime"
          type="datetime"
          value-format="YYYY-MM-DD HH:mm:ss"
          placeholder="选择督导时间"
        />
      </el-form-item>
      <el-form-item label="督导内容" prop="supervisionContent">
        <el-input
          v-model="formData.supervisionContent"
          type="textarea"
          :rows="4"
          placeholder="请输入督导内容"
        />
      </el-form-item>
      <el-form-item label="督导员" prop="supervisor">
        <el-input v-model="formData.supervisor" placeholder="请输入督导员姓名" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="formData.status" placeholder="请选择状态">
          <el-option label="计划中" :value="0" />
          <el-option label="进行中" :value="1" />
          <el-option label="已完成" :value="2" />
          <el-option label="已取消" :value="3" />
        </el-select>
      </el-form-item>
    </el-form>
    
    <template #footer>
      <el-button @click="submitForm" type="primary" :loading="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'

const emit = defineEmits(['success'])
const dialogVisible = ref(false)
const dialogTitle = computed(() => (formData.value.id ? '修改督导检查' : '添加督导检查'))
const formLoading = ref(false)
const formData = ref({
  id: undefined,
  supervisionTitle: '',
  supervisionType: undefined,
  supervisionObject: '',
  supervisionTime: '',
  supervisionContent: '',
  supervisor: '',
  status: 0
})
const formRules = ref({
  supervisionTitle: [{ required: true, message: '督导标题不能为空', trigger: 'blur' }],
  supervisionType: [{ required: true, message: '督导类型不能为空', trigger: 'change' }],
  supervisionObject: [{ required: true, message: '督导对象不能为空', trigger: 'blur' }],
  supervisionTime: [{ required: true, message: '督导时间不能为空', trigger: 'change' }]
})

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  resetForm()
  if (id) {
    formLoading.value = true
    try {
      // TODO: 加载督导详情
      formData.value.id = id
    } finally {
      formLoading.value = false
    }
  }
}

/** 提交表单 */
const submitForm = async () => {
  const elForm = unref(formRef)
  if (!elForm) return
  const valid = await elForm.validate()
  if (!valid) return
  formLoading.value = true
  try {
    const data = formData.value
    if (data.id) {
      // TODO: 修改督导检查
    } else {
      // TODO: 新增督导检查
    }
    dialogVisible.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    supervisionTitle: '',
    supervisionType: undefined,
    supervisionObject: '',
    supervisionTime: '',
    supervisionContent: '',
    supervisor: '',
    status: 0
  }
  nextTick(() => {
    formRef.value?.clearValidate()
  })
}

defineExpose({ open })
</script> 